package cmd

import (
	"context"
	"github.com/gogf/gf/v2/util/gconv"
	"lot_admin_api/internal/consts"
	"lot_admin_api/internal/router"
	"lot_admin_api/utility/middleware"
	"lot_admin_api/utility/response"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/os/gcmd"
)

var (
	Main = gcmd.Command{
		Name:  "main",
		Usage: "main",
		Brief: "start http server",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			s := g.Server()
			s.Use(middleware.CORS)
			r := g.RequestFromCtx(ctx)
			s.Group("/", func(group *ghttp.RouterGroup) {
				group.Group("/operate", func(group *ghttp.RouterGroup) {
					// 管理端token配置
					OperateGfToken := middleware.OperateGfToken()
					if err := OperateGfToken.Middleware(ctx, group); err != nil {
						response.JsonExit(r, consts.HTTPInternalServer.Code(), "商家端token函数错误")
					}
					// 管理端中间件配置
					group.Middleware(
						// 读取redis里面的token的员工详情参数
						func(r *ghttp.Request) {
							tokenInfo := OperateGfToken.GetTokenData(r).Data
							r.SetCtxVar(consts.LotToken, gconv.String(tokenInfo))
							r.Middleware.Next()
						},
						middleware.ResponseHandler, // 自定义返回参数
					)
					// 管理端路径
					router.OperateRouter(group)
				})
			})
			s.Run()
			return nil
		},
	}
)
